<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */
namespace App\Api\Controllers\CmbcUnion;


use App\Api\Controllers\Config\LinkageConfigController;
use App\Api\Controllers\Config\MyBankConfigController;
use App\Models\MemberList;
use App\Models\MemberSetJf;
use App\Models\MemberTpl;
use EasyWeChat\Factory;
use Illuminate\Http\Request;

class OauthController extends BaseController
{

    //授权
    public function oauth(Request $request)
    {
//        $sub_info = $request->get('state');
//        return redirect(url('api/cmbcunion/weixin/oauth_callback?sub_info=' . $sub_info . '&wx_AppId=111' . '&wx_Secret=111'. ''));
        $state = $this->decode($request->get('state'));
        $options = $this->Options($state['config_id']);
        $scope_type = $state['scope_type'];//snsapi_base snsapi_userinfo获取的信息不一样
        $sub_info = $request->get('state');
        $config = [
            'app_id' => $options['app_id'],
            'scope' => $scope_type,
            'oauth' => [
                'scopes' => [$scope_type],
                'response_type' => 'code',
                'callback' => url('/api/cmbcunion/weixin/oauth_callback?sub_info=' . $sub_info),
            ],
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;

        return $oauth->redirect();
    }


    public function oauth_callback(Request $request)
    {
//        $sub_info = $request->get('sub_info');
//
//        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
//        $store_id = $sub_info_arr['store_id'];
//        $store_name = $sub_info_arr['store_name'];
//        $merchant_id = $sub_info_arr['merchant_id'];
//        $users = $this->users($sub_info, $options);
//        $data = [
//            'store_id' => $store_id,
//            'store_name' => $store_name,
//            'store_address' => '',
//            'open_id' => $users->token->openid,
//            'merchant_id' => $merchant_id
//        ];
//
//        $data = base64_encode(json_encode((array)$data));
//
//        return redirect('/api/cmbcunion/weixin/pay_view?data=' . $data);
        $sub_info = $request->get('sub_info');
        $sub_info = $this->decode($sub_info);
        $options = $this->Options($sub_info['config_id']);
        $sub_info['code'] = $request->get('code');
        $users = $this->users($sub_info, $options);

        $store_id = $sub_info['store_id'];
        $store_name = $sub_info['store_name'];
        $merchant_id = $sub_info['merchant_id'];

        $data = [
            'store_id' => $store_id,
            'store_name' => $store_name,
            'store_address' => '',
            'open_id' => $users->token->openid,
            'merchant_id' => $merchant_id
        ];
        $data = base64_encode(json_encode((array)$data));
        $url = url('/api/cmbcunion/weixin/pay_view');
        header("Location: $url/?data=".$data);
    }
    
     public function unionpay(Request $request){
        $sub_info = $request->get('state');
        $sub_info_arr = json_decode(base64_decode((string)$sub_info), true);
        $store_id = $sub_info_arr['store_id'];
        $store_pid=0;
        $data = [
            'store_id' => $sub_info_arr['store_id'],
            'store_name' => $sub_info_arr['store_name'],
            'store_address' => '',
            'open_id' => 11,
            'merchant_id' => $sub_info_arr['merchant_id']
        ];

        return view('cmbcUnion.unionpay', compact('data'));
    }


    //支付显示页面
    public function pay_view(Request $request)
    {
        $data = json_decode(base64_decode((string)$request->get('data')), true);

        return view('cmbcUnion.weixin', compact('data'));
    }





}
